package org.slusk.thynwor.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

import org.slusk.thynwor.corestructures.Environment;

public class NetSelectorDialog extends JDialog {

	private Environment environment;
	
	private final JPanel contentPanel = new JPanel();
	private final ButtonGroup actionBtnGroup = new ButtonGroup();
	private final ButtonGroup movementBtnGroup = new ButtonGroup();
	private JRadioButton rdbtnOriginalHighlevelAction;
	private JRadioButton rdbtnLowlevelActionNet;
	private JRadioButton rdbtnNewHighlevelActionnet;
	private JRadioButton rdbtnOriginalTaskSelection;
	private JRadioButton rdbtnFuzzyVision;
	private final ButtonGroup fTtDBtnGroup = new ButtonGroup();
	private JRadioButton rdbtnAstar;
	private JRadioButton rdbtnHighlevelMoveNet2;
	private JRadioButton rdbtnHighlevelMoveNet1;
	private JRadioButton rdbtnRawLowLevel;
	private JRadioButton rdbtnFuzzyVisionBo4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NetSelectorDialog dialog = new NetSelectorDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public NetSelectorDialog() {
		setResizable(false);
		setTitle("Net Selector");
		setBounds(100, 100, 560, 445);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[grow][]", "[][][][][][][][][][][][][][][][]"));
		{
			JLabel lblActionNet = new JLabel("Action Net");
			contentPanel.add(lblActionNet, "cell 0 0");
		}
		{
			rdbtnOriginalHighlevelAction = new JRadioButton("Original High-Level Action Net");
			rdbtnOriginalHighlevelAction.setSelected(true);
			actionBtnGroup.add(rdbtnOriginalHighlevelAction);
			contentPanel.add(rdbtnOriginalHighlevelAction, "cell 0 1");
		}
		{
			JButton btnView = new JButton("View");
			contentPanel.add(btnView, "cell 1 1");
		}
		{
			rdbtnLowlevelActionNet = new JRadioButton("Low-Level Action Net");
			actionBtnGroup.add(rdbtnLowlevelActionNet);
			contentPanel.add(rdbtnLowlevelActionNet, "cell 0 2");
		}
		{
			JButton btnView_1 = new JButton("View");
			contentPanel.add(btnView_1, "cell 1 2");
		}
		{
			rdbtnNewHighlevelActionnet = new JRadioButton("New High-Level ActionNet");
			actionBtnGroup.add(rdbtnNewHighlevelActionnet);
			contentPanel.add(rdbtnNewHighlevelActionnet, "cell 0 3");
		}
		{
			JButton btnView_2 = new JButton("View");
			btnView_2.setEnabled(false);
			contentPanel.add(btnView_2, "cell 1 3");
		}
		{
			JLabel lblTaskSelection = new JLabel("Resource Selection / Movement Net Stacks");
			contentPanel.add(lblTaskSelection, "cell 0 5");
		}
		{
			rdbtnOriginalTaskSelection = new JRadioButton("Original Fuzzy Resource Selection");
			rdbtnOriginalTaskSelection.setSelected(true);
			movementBtnGroup.add(rdbtnOriginalTaskSelection);
			contentPanel.add(rdbtnOriginalTaskSelection, "cell 0 6");
		}
		{
			JButton btnView_3 = new JButton("View");
			contentPanel.add(btnView_3, "cell 1 6");
		}
		{
			rdbtnFuzzyVision = new JRadioButton("Fuzzy Vision / Fuzzy-Vision-To-Direction Stack");
			movementBtnGroup.add(rdbtnFuzzyVision);
			contentPanel.add(rdbtnFuzzyVision, "cell 0 7");
		}
		{
			JButton btnView_4 = new JButton("View");
			contentPanel.add(btnView_4, "cell 1 7");
		}
		{
			rdbtnRawLowLevel = new JRadioButton("Raw Low Level Vision Net");
			movementBtnGroup.add(rdbtnRawLowLevel);
			contentPanel.add(rdbtnRawLowLevel, "cell 0 8");
		}
		{
			rdbtnFuzzyVisionBo4 = new JRadioButton("Fuzzy Vision - Best of Four Regions Net");
			movementBtnGroup.add(rdbtnFuzzyVisionBo4);
			contentPanel.add(rdbtnFuzzyVisionBo4, "cell 0 9");
		}
		{
			JSeparator separator = new JSeparator();
			contentPanel.add(separator, "cell 0 10 2 1");
		}
		{
			JLabel lblFuzzytasktodirectionNets = new JLabel("Fuzzy-Resource-To-Direction Nets");
			contentPanel.add(lblFuzzytasktodirectionNets, "cell 0 12");
		}
		{
			rdbtnHighlevelMoveNet1 = new JRadioButton("High-Level Move Net 1");
			fTtDBtnGroup.add(rdbtnHighlevelMoveNet1);
			contentPanel.add(rdbtnHighlevelMoveNet1, "cell 0 13");
		}
		{
			rdbtnHighlevelMoveNet2 = new JRadioButton("High-Level Move Net 2");
			rdbtnHighlevelMoveNet2.setSelected(true);
			fTtDBtnGroup.add(rdbtnHighlevelMoveNet2);
			contentPanel.add(rdbtnHighlevelMoveNet2, "cell 0 14");
		}
		{
			rdbtnAstar = new JRadioButton("A-Star");
			fTtDBtnGroup.add(rdbtnAstar);
			contentPanel.add(rdbtnAstar, "cell 0 15");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						updateEnvironmentSettings();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	protected void updateEnvironmentSettings() {
//		if (rdbtnLowlevelActionNet.isSelected()) {
//			environment.setDroneActionMode(DroneActionMode.LL);
//		} else if (rdbtnOriginalHighlevelAction.isSelected()) {
//			environment.setDroneActionMode(DroneActionMode.ORIGHL);
//		} else {
//			environment.setDroneActionMode(DroneActionMode.NEWHL);
//		}
//		
//		if (rdbtnFuzzyVision.isSelected()) {
//			environment.setDroneMoveMode(DroneMoveMode.FUZZYVISIONSTACK);
//		} else if (rdbtnOriginalTaskSelection.isSelected()){
//			environment.setDroneMoveMode(DroneMoveMode.FUZZYTASKSELECTION);
//		} else if (rdbtnFuzzyVisionBo4.isSelected()){
//			environment.setDroneMoveMode(DroneMoveMode.FVBO4R);
//		} else {
//			environment.setDroneMoveMode(DroneMoveMode.RAWLLVISION);
//		}
//		
//		if (rdbtnAstar.isSelected()) {
//			environment.setDroneHlMoveMode(DroneHlMoveMode.ASTAR);
//		} else if (rdbtnHighlevelMoveNet1.isSelected()) {
//			environment.setDroneHlMoveMode(DroneHlMoveMode.HL1);
//		} else {
//			environment.setDroneHlMoveMode(DroneHlMoveMode.HL2);
//		}
//			
//		setVisible(false);
	}

	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}
	
	public void display() {
//		if (environment.getActionMode() == DroneActionMode.LL) {
//			rdbtnLowlevelActionNet.setSelected(true);
//		} else if (environment.getActionMode() == DroneActionMode.ORIGHL) {
//			rdbtnOriginalHighlevelAction.setSelected(true);
//		} else {
//			rdbtnNewHighlevelActionnet.setSelected(true);
//		}
//
//		if (environment.getMoveMode() == DroneMoveMode.FUZZYTASKSELECTION) {
//			rdbtnOriginalTaskSelection.setSelected(true);
//		} else if (environment.getMoveMode() == DroneMoveMode.FUZZYVISIONSTACK) {
//			rdbtnFuzzyVision.setSelected(true);
//		} else if (environment.getMoveMode() == DroneMoveMode.FVBO4R) {
//			rdbtnFuzzyVisionBo4.setSelected(true);
//		} else {
//			rdbtnRawLowLevel.setSelected(true);
//		}
//		
//		if (environment.getHlMoveMode() == DroneHlMoveMode.HL1) {
//			rdbtnHighlevelMoveNet1.setSelected(true);
//		} else if (environment.getHlMoveMode() == DroneHlMoveMode.HL2) {
//			rdbtnHighlevelMoveNet2.setSelected(true);
//		} else {
//			rdbtnAstar.setSelected(true);
//		}
//		
//		setVisible(true);
	}

	protected JRadioButton getRdbtnOriginalHighlevelAction() {
		return rdbtnOriginalHighlevelAction;
	}
	protected JRadioButton getRdbtnLowlevelActionNet() {
		return rdbtnLowlevelActionNet;
	}
	protected JRadioButton getRdbtnNewHighlevelActionnet() {
		return rdbtnNewHighlevelActionnet;
	}
	protected JRadioButton getRdbtnOriginalTaskSelection() {
		return rdbtnOriginalTaskSelection;
	}
	protected JRadioButton getRdbtnFuzzyVision() {
		return rdbtnFuzzyVision;
	}
	protected JRadioButton getRdbtnAstar() {
		return rdbtnAstar;
	}
	protected JRadioButton getRdbtnHighlevelMoveNet2() {
		return rdbtnHighlevelMoveNet2;
	}
	protected JRadioButton getRdbtnHighlevelMoveNet1() {
		return rdbtnHighlevelMoveNet1;
	}
	protected JRadioButton getRdbtnRawLowLevel() {
		return rdbtnRawLowLevel;
	}
	protected JRadioButton getRdbtnFuzzyVisionBo4() {
		return rdbtnFuzzyVisionBo4;
	}
}
